package org.example.likou150.section;

import java.util.ArrayList;
import java.util.List;

// https://leetcode.cn/problems/summary-ranges/description/?envType=study-plan-v2&envId=top-interview-150
public class SummaryRanges {
//    输入：nums = [0,1,2,4,5,7]
//    输出：["0->2","4->5","7"]

    public List<String> summaryRanges(int[] nums) {
        List<String> list=new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int fastIndex=i;
            while(fastIndex<nums.length-1 && nums[fastIndex]+1==nums[fastIndex+1]){
                fastIndex++;
            }
            StringBuilder sb = new StringBuilder();
            if(i==fastIndex){
                sb.append(nums[i]);
                list.add(sb.toString());
            }else{
                sb.append(nums[i]).append("->").append(nums[fastIndex]);
                list.add(sb.toString());
                i=fastIndex;
            }
        }
        return list;
    }
}
